package com.ruoyi.inspection.domain.receiveVo;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.system.domain.KeyPersonnelAdmin;
import com.ruoyi.system.domain.TemporaryStaffAccount;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

/**
 * 上报人员基础信息
 */
@Slf4j
@Data
public class ReceiveEmployee implements Serializable {
    //// 唯一编码，36 位uuid，必填
    private String id;
    //人员姓名
    private String personName;
    /**
    //人员编码 9 位企业编码+2 位人员类型编码+7 位企业内部流水号
    private String staffCode;
    // 人员类型 01 内部人员/员工 02 承包商 03 访客04 物品 05 车辆，必填
    **/
    // 人员类型: 01 内部人员/员工 02 承包商 03 访客
    private String personType;
    ///出生日期
    private String birthDate;
    //性别 0 男，1 女
    private String gender;
    //岗位名称
    private String positionName;
    //手机号
    private String mobileNumber;
    // 企业编码，同“危险化学品登记信息管理系统”中的企业编码一致，必填
    private String companyCode;
    //所属部门
    private String affiliatedDepartment;
    // 删除状态 0 正常 1 删除
    private String deleted;
    /**
     * 创建人姓名，必填
     */
    private String createBy;
    /**
     * 创建时间，必填 时间格式 yyyy-MM-dd HH:mm:ss
     */
    private String createDate;
    /**
     * 修改人
     */
    private String updateBy;
    /**
     * 修改时间
     */
    private String updateDate;


    public static ReceiveEmployee keyPersonnelAdminToReceiveEmployee(KeyPersonnelAdmin person) {
        String governId = person.getGovernId();
        governId = StringUtils.isEmpty(governId) ? UUID.randomUUID().toString() : governId;

        ReceiveEmployee vo = new ReceiveEmployee();
        vo.setId(governId);
        vo.setPersonName(person.getName());
        vo.setPersonType(UserTypeEnum.NEI_USER.code);
        // 出生日期
        String cardNumber = person.getCardNumber();
        String birthDay = StringUtils.isNotEmpty(cardNumber) && cardNumber.length()>15
                ? cardNumber.substring(6, 14) : "";
        Date birthDate = DateUtils.dateTime("yyyyMMdd", birthDay);
        birthDay = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", birthDate);
        if (StringUtils.isEmpty(birthDay)) {
            return null;
        }
        vo.setBirthDate(birthDay);
        vo.setGender("0");
        vo.setPositionName(StringUtils.isEmpty(person.getPost()) ? "生产巡检" : person.getPost());
        String phone = person.getPhone();
        phone = StringUtils.isEmpty(phone) ? InspectionConstant.DEFAULT_MOBILE : phone;
        vo.setMobileNumber(phone);
        vo.setCompanyCode(GlobalConstants.COMPANY_CODE);
        vo.setAffiliatedDepartment(StringUtils.isEmpty(person.getDepartmentName()) ? "安保部" : person.getDepartmentName());
        vo.setDeleted("0");
        String createTime = person.getCreatedDate()==null ? DateUtils.getTime() : DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", person.getCreatedDate());
        vo.setCreateBy(StringUtils.isEmpty(person.getCreatedBy()) ? "admin" : person.getCreatedBy());
        vo.setCreateDate(createTime);
        vo.setUpdateBy(StringUtils.isEmpty(person.getModifyBy()) ? "admin" : person.getModifyBy());
        vo.setUpdateDate(person.getModifyDate()==null ? createTime : DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", person.getModifyDate()));

        return vo;
    }

    public static ReceiveEmployee temporaryStaffAccountToReceiveEmployee(TemporaryStaffAccount person) {
        try {
            String governId = person.getGovernId();
            governId = StringUtils.isEmpty(governId) ? UUID.randomUUID().toString() : governId;

            ReceiveEmployee vo = new ReceiveEmployee();
            vo.setId(governId);
            vo.setPersonName(person.getName());
            vo.setPersonType(UserTypeEnum.valueOfCode(person.getVisitType()));
            // 出生日期
            String cardNumber = person.getCardNumber();
            String birthDay = StringUtils.isNotEmpty(cardNumber) && cardNumber.length()>15
                    ? cardNumber.substring(6, 15) : "";
            Date birthDate = DateUtils.dateTime("yyyyMMdd", birthDay);
            birthDay = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", birthDate);
            if (StringUtils.isEmpty(birthDay)) {
                return null;
            }
            vo.setBirthDate(birthDay);
            vo.setGender("0");
            vo.setPositionName(person.getPersonnelType());
            vo.setMobileNumber(person.getPhone());
            vo.setCompanyCode(GlobalConstants.COMPANY_CODE);
            vo.setAffiliatedDepartment(person.getVisitDepartment());
            vo.setDeleted("0");
            String createTime = person.getCreatedDate()==null ? DateUtils.getTime() : DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", person.getCreatedDate());
            vo.setCreateBy(StringUtils.isEmpty(person.getCreatedBy()) ? "admin" : person.getCreatedBy());
            vo.setCreateDate(createTime);
            vo.setUpdateBy(StringUtils.isEmpty(person.getModifyBy()) ? "admin" : person.getModifyBy());
            vo.setUpdateDate(person.getModifyDate()==null ? createTime : DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", person.getModifyDate()));

            person.setGovernId(governId);
            return vo;
        } catch (Exception e) {
            log.error("外部用户上传市局，出现问题, person: {}", person, e);
            return null;
        }
    }


    /**
     * fgcvbervws-80956793637830656-qqq-00x
     * @param id
     * @return
     */
    public static String enId(String id) {
        String a = "qyyhxxtxvvsw-" + id + "-";
        String a1 = StrUtil.fillAfter(a, 'y', 32);
        return a1;
    }


    enum UserTypeEnum {
        NEI_USER("01","内部人员"),
        OUT_USER("02","承包商"),
        VISIT_USER("03","外来访客");

        private String code;
        private String desc;

        UserTypeEnum(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public static String valueOfCode(String desc) {
            if (StringUtils.isEmpty(desc)) {
                return "03";
            }

            UserTypeEnum userTypeEnum = Arrays.stream(values()).filter(status -> status.desc.equals(desc)).findFirst().orElse(VISIT_USER);
            return userTypeEnum.code;
        }
    }


}
